package com.b2c.ftysys.domain;

import java.io.Serializable;

public class Page implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6034614581751895161L;

	/**
	 * now page
	 */
	private int nowPage = 1;

	/**
	 * the total page size
	 */
	private int message_count;

	/**
	 * the size of page
	 */
	private int page_size;

	// private int page_count;

	public Page() {

	}

	public Page(int message_count) throws OutOfBoundaryException{
		this(0, message_count, 0);
	}

	public Page(Page page) throws OutOfBoundaryException{
		this(page.getNowPage(), page.getMessage_count(), page.getPage_size());
	}

	/**
	 * Page
	 * 
	 * @param nowPage
	 *            this page num
	 * @param message_count
	 *            count of all info
	 * @param page_size
	 *            num of per page
	 * @throws OutOfBoundaryException 
	 */
	public Page(int nowPage, int message_count, int page_size) throws OutOfBoundaryException{

		if (message_count > 0 && page_size > 0) {
			setMessage_count(message_count);
			setPage_size(page_size);
			setNowPage(nowPage);
		} else if (page_size == 0 && message_count > 0) {
			setMessage_count(message_count);
			setPage_size(10);
			setNowPage(1);
		} else
			throw new OutOfBoundaryException();

	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}

	public int getNowPage() {
		return this.nowPage;
	}

	public void setMessage_count(int message_count) {
		this.message_count = message_count;
	}

	public int getMessage_count() {
		return this.message_count;
	}

	public void setPage_size(int page_size) {
		this.page_size = page_size;
	}

	public int getPage_size() {
		return this.page_size;
	}

	public int getPage_count() {

		if (message_count <= 0)
			return 0;
		else if (page_size <= 0) {
			setPage_size(10);
		}

		return this.message_count % this.page_size == 0 ? (this.message_count / this.page_size)
				: (this.message_count / this.page_size) + 1;
	}

	public int getStartOffset(){

		if (nowPage < 1)
			nowPage = 1;
		if (getPage_count() == 0 ? true : getNowPage() <= getPage_count()) {
			return ((this.nowPage - 1) * this.page_size);
		} else {
			nowPage = 1;
			return 0;
		}
	}

	public int[] getRowIndexMax(){
		int[] rowIndexMax = { this.getStartOffset(), this.getPage_size() };
		return rowIndexMax;
	}

}
